package test.test6_18;

public class Demo15_BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(binarySearch(arr,9));

    }

    public static int binarySearch(int[] arr, int findValueIndex) {
        int left = 0;//左边指针指向第一个数据

        int right = arr.length - 1;//右边指针指向第二的数据

        int mid = (left + right) / 2;//

        while (left <= right) {
            if (findValueIndex > mid) {
                left = mid + 1;
            }else if(findValueIndex < mid) {
                right = mid - 1;
            }else {
                return mid;
            }
            mid = (left + right)/2;
        }

        return -1;
    }
}


